import configparser


def read_config(file_path):
    # 创建ConfigParser对象
    config = configparser.RawConfigParser()
    config.optionxform = lambda option: option  # 确保在使用选项名时保持大小写不变

    # 读取配置文件时使用utf-8编码
    with open(file_path, 'r', encoding='utf-8') as config_file:
        config.read_file(config_file)

    # 初始化字典
    config_dict = {}

    # 遍历配置文件的各个section和key，将其添加到字典中
    for section in config.sections():
        config_dict[section] = {}
        for option in config.options(section):
            config_dict[section][option] = config.get(section, option)

    return config_dict


# 读取配置文件并将配置信息存储到字典中
config_file_path = 'relay_configuration(Not_Fully_automatic).ini'
config_data = read_config(config_file_path)

# 打印字典内容
print(config_data)

# 通过字典读取配置信息
Relay_board_1_Not_Fully_automatic = config_data['Relay_board_1_Not_Fully_automatic']
print(Relay_board_1_Not_Fully_automatic['A_Measuring_valve'])  # 选取配置项中某个配置
print(f"Value for Section1/key1: {Relay_board_1_Not_Fully_automatic}")
